package cn.ljn.org.controller;


import cn.ljn.basic.annotation.PreAuthorize;
import cn.ljn.basic.util.AjaxResult;
import cn.ljn.basic.util.PageList;
import cn.ljn.org.domain.Department;
import cn.ljn.org.query.DepartmentQuery;
import cn.ljn.org.service.IDepartmentService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/dept")
@RestController
//@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {
    @Autowired
    private IDepartmentService  departmentService;

    // 查询所有
    public List<Department> findAll() {
        return departmentService.findAll();
    }
    // 查询一个
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Department findById(@ApiParam(value="部门ID",required = true)  @PathVariable("id") Long id) {
        return departmentService.findById(id);
    }
    // 添加/修改
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department){
        if (null == department.getId()) {
            // 添加
            departmentService.add(department);
        } else {
            departmentService.update(department);
        }
        return AjaxResult.me().success();
    }
    // 删除
    @DeleteMapping("/{id}")
//    @PreAuthorize(name = "部门删除",sn = "department:delete")
    public AjaxResult delete(@PathVariable("id") Long id ){
        departmentService.delete(id);
        return AjaxResult.me().success();
    }
    // 分页
    @PostMapping
    public PageList<Department> queryByPage(@RequestBody DepartmentQuery query) {
        return departmentService.queryByPage(query);
    }

    // 批量删除
    @PatchMapping("/batch")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
         departmentService.batchDelete(ids);
        return AjaxResult.me().success();
    }

    /**
     * 获取部门树
     */
    @GetMapping("/tree")
//    @PreAuthorize(name="部门树",sn="department:tree")
    public List<Department> tree() {
        return departmentService.tree();
    }

}
